#include "stdafx.h"
#include "Tree.h"

#include "../../Framework/ContentManager.h"

namespace MyGame {

Tree::Tree(Game* game) : Mesh(game) {
    this->meshFileLOD = NULL;
    this->meshLOD = NULL;
    this->SetCullMode(D3DCULL_NONE);
    this->SetEffectFile("AmbDiff.fx");
}

Tree::~Tree(void) {
}

LPCSTR Tree::GetMeshFileLOD() const {
    return this->meshFileLOD;
}

void Tree::SetMeshFileLOD(LPCSTR val) {
    this->meshFileLOD = val;
}

D3DXMesh* Tree::GetMeshLOD() const {
    return this->meshLOD;
}

void Tree::Draw(FLOAT timeDelta) {
    D3DXVECTOR3 vLightDir = D3DXVECTOR3(1, 1, 1);
    D3DXVec3Normalize(&vLightDir, &vLightDir);
    D3DXCOLOR vLightDiffuse = D3DXCOLOR(1, 1, 1, 1);
    this->effect->SetValue("g_LightDir", &vLightDir, sizeof(D3DXVECTOR3));
    this->effect->SetValue("g_LightDiffuse", &vLightDiffuse, sizeof(D3DXCOLOR)) ;

    D3DXMATRIX mWorld = this->GetWorldMatrix();
    D3DXMATRIX mWorldViewProjection = mWorld * this->GetGame()->GetCamera()->GetViewMatrix() * this->GetGame()->GetCamera()->GetProjectionMatrix();
    this->effect->SetMatrix("g_mWorldViewProjection", &mWorldViewProjection) ;
    this->effect->SetMatrix("g_mWorld", &mWorld) ;

    D3DXCOLOR colorMtrlAmbient(0.1f, 0.1f, 0.1f, 1.0f);
    D3DXCOLOR colorMtrlDiffuse(1.0f, 1.0f, 1.0f, 1.0f);
    this->effect->SetValue("g_MaterialAmbientColor", &colorMtrlAmbient, sizeof(D3DXCOLOR));
    this->effect->SetValue("g_MaterialDiffuseColor", &colorMtrlDiffuse, sizeof(D3DXCOLOR));

    this->effect->SetTechnique("Tree_Shader_Normal_Alpha");
    __super::Draw(timeDelta);
}

void Tree::DrawLodModel(FLOAT timeDelta) {
    D3DXMesh* m = this->mesh;
    this->mesh = this->meshLOD;
    this->Draw(timeDelta);
    this->mesh = m;
}

void Tree::LoadContent() {
    __super::LoadContent();

    if (this->meshFileLOD != NULL) {
        this->meshLOD = this->GetGame()->GetContentManager()->LoadMesh(this->meshFileLOD);
    }
}

}
